package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOP.service.util.MathUtil;
import com.burgeon.backend.SHOPBACKEND.entity.booking.BactiviteRecord;
import com.burgeon.backend.SHOPBACKEND.entity.booking.Bactivitie;
import com.burgeon.backend.SHOPBACKEND.entity.booking.BookingStoreData;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendBookingActivitiesMapper;
import com.burgeon.backend.SHOPBACKEND.service.BackendBookingActivitiesService;
import com.burgeon.backend.SHOPBACKEND.service.ex.ErrorDataException;
import com.burgeon.backend.SHOPBACKEND.service.ex.UploadException;
import com.burgeon.backend.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

@Service
public class BackendBookingActivitiesServiceImpl implements BackendBookingActivitiesService {
    @Autowired
    private BackendBookingActivitiesMapper backendBookingActivitiesMapper;

    /**
     * 记录查找
     * @param field A会员名 B会员手机
     * @param value
     * @param start
     * @param account
     * @return
     */
    @Override
    public HashMap findBactiviteRecord(String field, String value, Integer start, String account) {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("A","name");
        map.put("B","phone");

        HashMap data = new HashMap();
        List<BactiviteRecord> bactiviteRecord = backendBookingActivitiesMapper.findBactiviteRecord(map.get(field),value,start * 20, account);
        for (BactiviteRecord record : bactiviteRecord){
            User user = backendBookingActivitiesMapper.getUserById(record.getUid());
            record.setVipImg(user.getImg());
            record.setVipName(user.getName());
            record.setVipPhone(user.getPhone());

        }
        data.put("list",bactiviteRecord);
        data.put("count",backendBookingActivitiesMapper.findBactiviteRecordCount(map.get(field),value,account));


        return data;
    }

    @Override
    public HashMap getBactiviteRecord(Integer start,String account) {
        HashMap data = new HashMap();
        List<BactiviteRecord> bactiviteRecord = backendBookingActivitiesMapper.getBactiviteRecord(start * 20, account);
        for (BactiviteRecord record : bactiviteRecord){
            User user = backendBookingActivitiesMapper.getUserById(record.getUid());
            record.setVipImg(user.getImg());
            record.setVipName(user.getName());
            record.setVipPhone(user.getPhone());

        }
        data.put("list",bactiviteRecord);
        data.put("count",backendBookingActivitiesMapper.getBactiviteRecordCount(account));

        return data;
    }

    @Override
    public List<Bactivitie> getBookingListData(String account) {
        List<Bactivitie> bookingListData = backendBookingActivitiesMapper.getBookingListData(account);

        for (Bactivitie bactivitie:bookingListData){
            BigDecimal applyRate = MathUtil.divide(new BigDecimal(bactivitie.getApplyCount()),new BigDecimal(bactivitie.getCount()));
            BigDecimal finishRate = MathUtil.divide(new BigDecimal(bactivitie.getFinishCount()),new BigDecimal(bactivitie.getCount()));
            bactivitie.setApplyRate(applyRate);
            bactivitie.setFinishRate(finishRate);

        }
        return bookingListData;
    }

    @Override
    public List<BookingStoreData> getStoreData(String account) {
        return backendBookingActivitiesMapper.getBookingStoreData(account);
    }

    @Override
    @Transactional
    public void addData(String name,String stateTime, String endTime, String[] stores, Integer count, String account,String explain, MultipartFile img) throws ErrorDataException, UploadException {

        Bactivitie bactivitie = new Bactivitie();

        bactivitie.setStateTime(stateTime);
        bactivitie.setEndTime(endTime);
        bactivitie.setName(name);
        bactivitie.setCount(count);
        bactivitie.setAccount(account);
        bactivitie.setExplain(explain);

        backendBookingActivitiesMapper.addData(bactivitie);
        String bid = bactivitie.getId();

        if (null == img || img.isEmpty() ) {
            System.out.println("活动图片为空");
            throw new UploadException("活动图片为空");
        }

        String fileName = img.getOriginalFilename(); //文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));// 后缀名

        String filePath = "//root//code//img//" + account + "-shop//BookingActivities//"; // 上传后的路径
        fileName = bid + suffixName; // 新文件名

        //存入数据库
        String imgP = "http://jxfscdn.cloudsmicro.com/img/" + account + "-shop/BookingActivities/" + fileName;
        saveFile(img,fileName,filePath);

//        String fileNameItem = itemImg.getOriginalFilename(); //文件名
//        String suffixNameItem = fileNameItem.substring(fileNameItem.lastIndexOf("."));// 后缀名
//
//        String filePathItem = "//root//code//img//" + account + "-shop//BookingActivities//"; // 上传后的路径
//        fileNameItem = bid+"-item" + suffixNameItem; // 新文件名
//        String itemImgP = "http://jxfscdn.cloudsmicro.com/img/" + account + "-shop/BookingActivities/" + fileNameItem;
//        saveFile(itemImg,fileNameItem,filePathItem);

        backendBookingActivitiesMapper.upDataImg(bid,imgP,null);

        for (String sid :stores){
            backendBookingActivitiesMapper.addStoreItem(bid,sid);
        }


    }

    //保存文件
    private void saveFile(MultipartFile multipartFile, String fileName, String filePath) throws UploadException {
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            multipartFile.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            throw new UploadException("系统错误");
        } catch (Exception e) {
            System.err.println(e.getMessage());
            throw new UploadException("系统错误");
        }
    }
}
